package com.qding.park.manager.sms.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.qding.park.common.utils.http.SimpleHttpParam;
import com.qding.park.common.utils.http.SimpleHttpResult;
import com.qding.park.common.utils.http.SimpleHttpUtils;
import com.qding.park.manager.pojo.sms.ResultCodeEnum;
import com.qding.park.manager.pojo.sms.SmsResult;
import com.qding.park.manager.pojo.sms.device.InviteVisitNotice;
import com.qding.park.manager.pojo.sms.device.OutCheckAlarm;
import com.qding.park.manager.sms.DevicePushInterfaceService;
import com.qding.park.manager.sms.config.IMUrlConfig;

@Service("devicePushInterfaceService")
public class DevicePushInterfaceServiceImpl implements DevicePushInterfaceService {

	private static final Log _log = LogFactory.getLog(DevicePushInterfaceServiceImpl.class);
	
	
	private static enum PushTypeEnum{
		
		inviteVisitNotice(0,"邀请车辆来访"),
		outCheckAlarm(1,"车辆出场报警");
		private int type;
		
		private String desc;
		
		private PushTypeEnum(int type,String desc){
			this.type=type;
			this.desc=desc;
		}

		public int getType() {
			return type;
		}

		public void setType(int type) {
			this.type = type;
		}

		public String getDesc() {
			return desc;
		}

		public void setDesc(String desc) {
			this.desc = desc;
		}
		
		
	}

//	private static final String url = "http://qaboss.qdingnet.com/qding-openapi/openapi/json/machine/pushRequest";
	
	private static final String url=IMUrlConfig.getDevicePushUrl();

	public class PushRequest {

		private List<String> mids;

		private String tid;

		Map<String, String> params;

		public List<String> getMids() {
			return mids;
		}

		public void setMids(List<String> mids) {
			this.mids = mids;
		}

		public String getTid() {
			return tid;
		}

		public void setTid(String tid) {
			this.tid = tid;
		}

		public Map<String, String> getParams() {
			return params;
		}

		public void setParams(Map<String, String> params) {
			this.params = params;
		}

	}
	
	/**
	 * devicePush(这里用一句话描述这个方法的作用)
	 *
	 * @Title: devicePush @Description: TODO @param 设定文件 @return void
	 *         返回类型 @throws
	 */
	private void devicePush(String memberId,String action,String title) {
		String tid = "car_push";// 固定不变
		List<String> mids = new ArrayList<String>();
		mids.add(memberId);

		PushRequest request = new PushRequest();

		request.setMids(mids);
		request.setTid(tid);
		Map<String, String> params = new HashMap<String, String>();
		
		params.put("curTime", String.valueOf(System.currentTimeMillis() / 1000));// 固定不变
		params.put("carAction", action);// 模板变量
		params.put("alerttitle", title);// 模板变量
		
		request.setParams(params);
		
		
		Map<String,Object> parameters = new LinkedHashMap<String,Object>();
		String bodyStr=JSONObject.toJSONString(request);
		System.out.println("request params:"+bodyStr);
		parameters.put("body", bodyStr);
		_log.info("url:"+url);
		_log.info("bodyStr:"+bodyStr);
		SimpleHttpParam httpParam=new SimpleHttpParam(url);
		httpParam.setMethod(SimpleHttpUtils.HTTP_METHOD_POST);
		httpParam.setParameters(parameters);
		SimpleHttpResult  result=SimpleHttpUtils.httpRequest(httpParam);
		
		_log.info("code:" + result.getStatusCode());
		_log.info("Content:" + result.getContent());
		
		 
	}
	/*
	 * <p>Title: inviteVisitNotice</p> <p>Description:邀请来访通知 </p>
	 * 
	 * @param notice
	 * 
	 * @return
	 * 
	 * @see
	 * com.qding.park.manager.sms.DevicePushInterfaceService#inviteVisitNotice(
	 * com.qding.park.manager.pojo.sms.device.InviteVisitNotice)
	 */

	@Override
	public SmsResult inviteVisitNotice(InviteVisitNotice notice) {
		// TODO Auto-generated method stub

		_log.info("您邀请的" + notice.getCarNum() + "车辆已进入园区，请准备接待。");
		String title="您邀请的" + notice.getCarNum() + "车辆已进入园区，请准备接待。";
		JSONObject  msgjson=new JSONObject();
		msgjson.put("carNum", notice.getCarNum());
		msgjson.put("userId", notice.getMemberId());
		msgjson.put("type", PushTypeEnum.inviteVisitNotice.getType());
		devicePush(notice.getMemberId(),msgjson.toJSONString(),title);
		SmsResult resut = new SmsResult();
		resut.setCode(ResultCodeEnum.SUCCESS.getCode());
		resut.setMessage("发送成功");
		return resut;
	}

	/*
	 * <p>Title: outCheckAlarm</p> <p>Description: 车辆出场报警通知</p>
	 * 
	 * @param alarm
	 * 
	 * @return
	 * 
	 * @see
	 * com.qding.park.manager.sms.DevicePushInterfaceService#outCheckAlarm(com.
	 * qding.park.manager.pojo.sms.device.OutCheckAlarm)
	 */

	@Override
	public SmsResult outCheckAlarm(OutCheckAlarm alarm) {
		_log.info("检测到您的" + alarm.getCarNum() + "车辆有出场行为，请及时确认。");
		String title="检测到您的" + alarm.getCarNum() + "车辆有出场行为，请及时确认。";
		JSONObject  msgjson=new JSONObject();
		msgjson.put("carNum", alarm.getCarNum());
		msgjson.put("userId", alarm.getMemberId());
		msgjson.put("type", PushTypeEnum.outCheckAlarm.getType());
		devicePush(alarm.getMemberId(),msgjson.toJSONString(),title);
		SmsResult resut = new SmsResult();
		resut.setCode(ResultCodeEnum.SUCCESS.getCode());
		resut.setMessage("发送成功");
		return resut;
	}

}
